package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.entity.ResultMenu;

import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.RoleService;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashSet;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/menu")
@CrossOrigin
public class MenuController {
    @Reference
    private UserService userService;
    @Reference
    private RoleService roleService;
    @Reference
    private MenuService menuService;

    /**
     * 基于角色获取关联菜单
     *
     * @return
     */
    @GetMapping("/getAllMenuByRole")
    public Result getAllMenuByRole() {
        //第一步，从登录框架中，取出当前登录的用户是谁，角色是谁
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //如果没取出来对象，则用户未登录，直接retuen
        if (ObjectUtils.isEmpty(principal)) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
        //取出来对象，直接转成SpringSecurity的User对象
        User user = (User) principal;
        //获取当前登录的用户名，根据用户名查出用户是谁
        String username = user.getUsername();
        log.info("[当前登录用户为]username:{}", user.getUsername());
        //取出用户是谁
        com.itheima.health.pojo.User realUser = userService.findByUsername(username);
        //根据用户id，查出此用户角色为哪些
        List<Role> roles = roleService.findAllRoleByUserId(realUser.getId());
        for (Role role : roles) {
            log.info("[用户当前拥有的角色为]:{}", role);
        }
        //根据角色，查询出来所有的可显示菜单，放入返回集中返回
        LinkedHashSet<ResultMenu> menus = menuService.findAllMenuByRole(roles);
        return new Result(true, MessageConst.GET_MENU_SUCCESS, menus);
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[菜单-分页查询]data:{}", queryPageBean);
        //rpc查询结果
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true, "查询菜单信息成功!", pageResult);
    }

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu) {
        log.info("[菜单-添加]data:{}", menu);
        //rpc调用添加
        menuService.add(menu);
        return new Result(true, "添加菜单信息成功!");
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result deleteById(Integer id){
        log.info("[菜单-删除]data:{}");
        //rpc调用删除
        menuService.deleteById(id);
        return new Result(true,"删除菜单信息成功!");
    }




    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result edit(@RequestBody  Menu menu){
        log.info("[菜单编辑-更改]data:{}",menu);
        menuService.update(menu);
        return new Result(true,"更改编辑信息成功!");
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findById(Integer id){
        log.info("[菜单编辑-查询]data:{}");
        Menu menus = menuService.findById(id);
        return new Result(true,"查询编辑信息成功!",menus);
    }


}